/*
*	轮播图中 添加鼠标进入 , 移出事件
*/

const ShowOrHidden = function() {
	let box = document.getElementsByClassName('all')[0];//获取大盒子
    let left = document.getElementsByClassName('icon-angle-circle-left')[0];//获取上一页
    let right = document.getElementsByClassName('icon-angle-circle-right')[0];//获取下一页
    let screen_ul = document.getElementsByClassName('screen_ul')[0];//获取大盒子
    let current_index = 2;						// 记录当前播放的是哪一张图片
    let current_semaphore = 0;						// 设置轮播图 , 移动互斥型号量
    let pointer_index_current=1;					// 橙色指针下标
    let pointer_current = document.getElementsByClassName('pointer_current')[0];	// 橙色指针


    // 设置自动播放
    let Timeout = setInterval(function(){
    	GoToNext();
    },2800);


    let mouseover = function() {	// 鼠标进入事件
        left.style.opacity = 1;
        right.style.opacity = 1;
        clearInterval(Timeout);
    }

    let mouseout = function() {		// 鼠标离开事件
        left.style.opacity = 0;
        right.style.opacity = 0;
		Timeout = setInterval(function(){
		    GoToNext();
		},2800);
    }

    addEventListener(box,'mouseover',mouseover,true);
    addEventListener(box,'mouseout',mouseout,true);

	//移动函数
    function animationSlow(ele,currentOffset,target,fn=null,direction){


        let sum = currentOffset;			// 

    	if(currentOffset>target){
	        ele.timeID = setInterval(function(){

	            ele.style.left = sum-1 + 'vw';
	            sum = sum-1;
	            
	            if( sum == target){
	            	setIndex(direction);
	                clearInterval(ele.timeID); // 终点检测
	                setSemaphore();
	            }
	        },5);
    	}else {
	        ele.timeID = setInterval(function(){

	            ele.style.left = sum+1 + 'vw';
	            sum = sum+1;
	            
	            if( sum == target){
	            	setIndex(direction);
	                clearInterval(ele.timeID); // 终点检测
	                setSemaphore();
	            }
	        },5);
    	}


        function setSemaphore(){ 	//还原互斥型号量 ,  触发回调函数的
        	current_semaphore = 0;
        	MovePointer(direction);
        	if(fn!=null){
        		fn();
        	}
        }
        function setIndex(e) {		//跟新 current_index 值
        	if(e==0){
        		current_index=current_index+1;
        	}else{
        		current_index=current_index-1;
        	}
        }
    }


    // 点击下一页
    let GoToNext = function () {
    	if(current_semaphore==0) {
    		let thisDistance = (current_index-1)*(-100);
    		current_semaphore=1;
    		animationSlow(screen_ul,thisDistance,thisDistance-100,initialize,0);
    		// initialize();
    	}
    }

    addEventListener(right,'click',GoToNext,true);

    // 点击上一页
    let GoToPrev = function () {
    	if(current_semaphore==0) {
    		current_semaphore=1;
    		let thisDistance = (current_index-1)*(-100);
    		animationSlow(screen_ul,thisDistance,thisDistance+100,ToTheEnd,1);

    	}
    }
    addEventListener(left,'click',GoToPrev,true);



    function initialize (){		// 处理图片下标到最后一张 ( 跳转到第二张)
    	if (current_index == 5) {
    		current_semaphore = 1;
    		screen_ul.style.left=(-100)+'vw';
    		current_semaphore = 0;
    		current_index = 2;
    	}
    }

    function ToTheEnd (){			// 处理图片下标到第一张 ( 跳转到倒数第二张)
    	if (current_index == 1) {
    		current_semaphore = 1;
    		screen_ul.style.left=(-300)+'vw';
    		current_semaphore = 0;
    		current_index = 4;
    	}
    }

//	移动橙色指针的 偷个懒
	function MovePointer (e) {

		if(e==0){
			if (pointer_index_current==1) {
				move(1,2);
			}else if(pointer_index_current==2) {
				move(2,3);
			}else if (pointer_index_current==3) {
				move(3,1);
			}
		}else if (e==1) {
			if (pointer_index_current==1) {
				move(1,3);
			}else if(pointer_index_current==2) {
				move(2,1);
			}else if (pointer_index_current==3) {
				move(3,2);
			}
		}

			function move (this_,next){

				if(this_==1&&next==2){
					addClassName(pointer_current,'to_50');	
					pointer_index_current=2;
				}else if (this_==2&&next==3) {
					pointer_index_current=3;	
					addClassName(pointer_current,'to_100');
					delClassName(pointer_current,'to_50');
				}else if(this_==3&&next==1) {
					pointer_index_current=1;	
					delClassName(pointer_current,'to_100');
				}else if(this_==1&&next==3) {
					pointer_index_current=3;	
					addClassName(pointer_current,'to_100');
				}else if(this_==2&&next==1){
					pointer_index_current=1;	
					delClassName(pointer_current,'to_50');
					delClassName(pointer_current,'to_100');
				}else if(this_==3&&next==2) {
					pointer_index_current=2;	
					addClassName(pointer_current,'to_50');
					delClassName(pointer_current,'to_100');
				}
			}
	}

}


/*
*	等到页面加载完毕 , 再调用其方法
*/
	
window.onload=function(){

	ShowOrHidden();
	
}

